package com.test.liuhb.test;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import coma.test.liuhb.pojo.Fruit;
import coma.test.liuhb.pojo.FruitAdapter;

/**
 * Created by liu.hb on 2017/12/15.
 */

public class RecyclerViewActivity extends Activity {
    private List<Fruit> fruitList=new ArrayList<>();
    RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview);
        initFruits();
        recyclerView=(RecyclerView)findViewById(R.id.RecyclerView);
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(
                3,StaggeredGridLayoutManager.VERTICAL);
//        线性布局横向
//        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
//        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //        瀑布流布局纵向
        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter adapter=new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);
    }

    private void initFruits() {
        for(int i=0;i<2;i++){
//            线性布局横向
//            Fruit apple=new Fruit("Apple");
//            fruitList.add(apple);
//            Fruit banana=new Fruit("Banana");
//            fruitList.add(banana);
//            Fruit orange=new Fruit("Orange");
//            fruitList.add(orange);
//            Fruit pear=new Fruit("Pear");
//            fruitList.add(pear);
            //        瀑布流布局纵向
            Fruit apple=new Fruit(getRandomLengthName("Apple"));
            fruitList.add(apple);
            Fruit banana=new Fruit(getRandomLengthName("Banana"));
            fruitList.add(banana);
            Fruit orange=new Fruit(getRandomLengthName("Orange"));
            fruitList.add(orange);
            Fruit pear=new Fruit(getRandomLengthName("Pear"));
            fruitList.add(pear);
        }
    }
    private String getRandomLengthName(String Name){
        Random random=new Random();
        int length=random.nextInt(20)+1;
        StringBuilder builder=new StringBuilder();
        for(int i=0;i<length;i++){
            builder.append(Name);
        }
        return builder.toString();
    }
}
